//
//  DDHeadLineController.m
//  NetEaseNews
//
//  Created by dqw on 15/7/24.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "DDHeadLineController.h"
#import "DDHeadLine.h"
#import "DDHeadLineCell.h"
@interface DDHeadLineController ()
@property (strong, nonatomic) NSArray *headlines;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (assign, nonatomic) NSInteger currentIndex;

@end

@implementation DDHeadLineController

static NSString * const reuseIdentifier = @"headLine";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureFlowLayout];
    [DDHeadLine headLinesWithCompletionB:^(NSArray *arrM) {
        self.headlines = arrM;
    }];
}
- (void)scrollToSecondItem
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];
}
- (void)setHeadlines:(NSArray *)headlines
{
    _headlines = headlines;
    [self.collectionView reloadData];
    [self scrollToSecondItem];
}
- (void)configureFlowLayout
{
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.flowLayout.itemSize = self.view.bounds.size;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.headlines.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DDHeadLineCell *cell = (DDHeadLineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSInteger index = (indexPath.item -1 + self.currentIndex + self.headlines.count) % self.headlines.count;
    NSLog(@"curIndex %d" , self.currentIndex);
    NSLog(@"item %d", indexPath.item);
    NSLog(@"index %d", index);
    cell.headline = self.headlines[index];
    cell.tag = index;
    
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.currentIndex = (offset - 1 + self.currentIndex + self.headlines.count) % self.headlines.count;
    
    [self scrollToSecondItem];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
